package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.constants.Constants;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.entity.vo.UserInfoVo;
import com.easychat.service.UserInfoService;
import com.easychat.utils.StringTools;
import com.easychat.websocket.netty.ChannelContextUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.IOException;

/**
 * @Author Dongdong
 * @注释
 */


@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends ABaseController {
    @Resource
    private ChannelContextUtils channelContextUtils;
    @Resource
    private UserInfoService userInfoService;

    /**
     * ’账号设置‘中展示的用户信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/getUserInfo")
    @GlobalInterceptor
    public ResponseVO getUserInfo(HttpServletRequest request) {

        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserInfo userInfo = userInfoService.getUserInfoByUserId(tokenUserInfo.getUserId());
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);

        return getSuccessResponseVO(userInfoVo);
    }

    /**
     * 保存用户修改的信息
     * @param userInfo 前端传过来的userInfo
     * @param avatarFile 头像文件
     * @param avatarCover 头像封面文件
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/saveUserInfo")
    @GlobalInterceptor
    public ResponseVO saveUserInfo(UserInfo userInfo,
                                   MultipartFile avatarFile,
                                   MultipartFile avatarCover,
                                   HttpServletRequest request) throws IOException {

        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        userInfo.setUserId(tokenUserInfo.getUserId());

        //设置为null，防止穿透进行修改用户的信息
        userInfo.setPassword(null);
        userInfo.setEmail(null);
        userInfo.setCreateTime(null);
        userInfo.setLastLoginTime(null);
        userInfo.setStatus(null);
        userInfoService.updateUserInfo(userInfo, avatarFile, avatarCover);

        //直接返回这个方法，让他更新完实时更新一次
        return getUserInfo(request);
    }

    /**
     * 修改密码
     * @param password 密码
     * @param request
     * @return
     */
    @RequestMapping("/updatePassword")
    @GlobalInterceptor
    public ResponseVO updatePassword(@NotEmpty @Pattern(regexp = Constants.REGX_PASSWORD) String password,
                                     HttpServletRequest request) {

        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

        UserInfo userInfo = new UserInfo();
        String encodePassword = StringTools.encodeMD5(password);
        userInfo.setPassword(encodePassword);
        userInfoService.updateUserInfoByUserId(userInfo, tokenUserInfo.getUserId());
        //  强制退出 重新登陆
        channelContextUtils.closeContact(tokenUserInfo.getUserId());
        return getSuccessResponseVO(null);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    @GlobalInterceptor
    public ResponseVO logout(HttpServletRequest request) {

        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

        //退出登录，关闭ws连接
        channelContextUtils.closeContact(tokenUserInfo.getUserId());
        return getSuccessResponseVO(null);
    }


}
